package com.cxps.server.role.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.role.domain.Role;
import com.cxps.server.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 角色前端控制器
 * </p>
 *
 * @author YouCai.Liu
 * @since 2021-10-18
 */
@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色API")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     *
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "角色列表", response = ApiResponse.class)
    public ApiResponse list() {
        return ApiResponse.fireSuccess(roleService.list());
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加角色", response = ApiResponse.class)
    public ApiResponse add(@RequestBody Role role) {
        roleService.add(role);
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据ID查询角色
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID查询角色", response = ApiResponse.class)
    public ApiResponse getRoleById(@PathVariable String id) {
        return ApiResponse.fireSuccess(roleService.getRoleById(id));
    }

    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改角色", response = ApiResponse.class)
    public ApiResponse update(@RequestBody Role role) {
        try {
            Assert.hasLength(role.getId(), "ID不能为空");
            roleService.update(role);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @GetMapping("/del")
    @ApiOperation(value = "删除角色", response = ApiResponse.class)
    public ApiResponse batchDel(@RequestParam String[] ids) {
        try {
            Assert.notEmpty(ids, "IDS不能为空");
            roleService.batchDel(ids);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 添加权限
     *
     * @param id
     * @param menus
     * @return
     */
    @GetMapping("/addMenu")
    @ApiOperation(value = "添加权限", response = ApiResponse.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "menus", value = "权限ID集合", required = true, dataType = "array", paramType = "query")
    })
    public ApiResponse addMenu(@RequestParam("id") String id, @RequestParam("menus") String[] menus) {
        try {
            Assert.hasLength(id, "id can not be empty！");
            Assert.notEmpty(menus, "menus can not be empty！");
            roleService.addMenu(id, menus);
        } catch (IllegalArgumentException e) {
            return ApiResponse.fireError(e.getMessage());
        }
        return ApiResponse.fireSuccess();
    }

    /**
     * 角色集合信息
     *
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "角色集合信息", response = ApiResponse.class)
    public ApiResponse getRoles() {
        return ApiResponse.fireSuccess(roleService.getRoles());
    }

    /**
     * 根据ID查询角色权限集合
     *
     * @param id
     * @return
     */
    @GetMapping("/getRoleMenu/{id}")
    @ApiOperation(value = "根据ID查询角色权限ID集合", response = ApiResponse.class)
    public ApiResponse getRoleMenuByRoleId(@PathVariable String id) {
        return ApiResponse.fireSuccess(roleService.getRoleMenuByRoleId(id));
    }

}

